package models

import "log"

// 获取所有菜单
func GetAllMenu() []interface{} {
	dataList := make([]interface{}, 0)
	strSql := "select menu_id,name,icon,url,parent_id,sort from sys_menus where menu_type=0 order by sort"
	db, _ := OpenDB()
	defer db.Close()
	rows, err1 := db.Queryx(strSql)
	if err1 != nil {
		log.Fatalln(err1)
		return dataList
	}
	for rows.Next() {
		data := make(map[string]interface{})
		rows.MapScan(data)
		dataList = append(dataList, data)
	}
	return dataList
}

// 获取用户菜单
func GetMenuByUserId(userId int64) []interface{} {
	dataList := make([]interface{}, 0)
	strSql := `select menu_id,name,icon,url,parent_id,sort
                from sys_menus where menu_type = 0 and menu_id in (select a.auth_id 
                    from sys_user_role r join sys_auth a on a.obj_type =0 and a.auth_type = 0   and r.role_id =a.obj_id 
                    where r.user_id =$1
                    union all 
                    select a.auth_id 
                    from  sys_auth a    
                    where  a.obj_type =1 and a.auth_type = 0  and a.obj_id  =$1
                ) order by sort, menu_id`

	db, _ := OpenDB()
	defer db.Close()
	rows, err1 := db.Queryx(strSql, userId)
	if err1 != nil {
		log.Fatalln(err1)
		return dataList
	}
	for rows.Next() {
		data := make(map[string]interface{})
		rows.MapScan(data)
		dataList = append(dataList, data)
	}
	return dataList
}
